package com.lixingqu.mvpdemo.net;

import android.net.Network;
import android.util.Log;

import com.lixingqu.mvpdemo.BuildConfig;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lixingqu on 2016/12/2.
 */

public class NetWorks {
    private static final int DEFAULT_TIMEOUT = 5;
    private static Retrofit mRetrofit;
    private static NetWorks mNetWorks;

    private NetWorks() {
    }

    public static NetWorks getInstance() {
        if (mNetWorks == null) {
            synchronized (Network.class) {
                if (mNetWorks == null) {
                    mNetWorks = new NetWorks();
                }
            }
        }
        return mNetWorks;
    }

    public HttpService getHttpService() {
        return configRetrofit(HttpService.class);
    }

    public DownloadService getDownLoadService() {
        return configRetrofit(DownloadService.class);
    }

    private static <T> T configRetrofit(Class<T> service) {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.API_BASE_URL)
                .client(configClient())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return mRetrofit.create(service);
    }

    private static OkHttpClient configClient() {
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
        Interceptor headerInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder builder = chain.request().newBuilder();
                builder.addHeader("apikey", "16a0733453d6136b61031bf04b504593");
                return chain.proceed(builder.build());
            }
        };

        Interceptor loggingIntercept = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Response response = chain.proceed(request);
                ResponseBody responseBody = response.body();
                BufferedSource source = responseBody.source();
                source.request(Long.MAX_VALUE); // Buffer the entire body.
                Buffer buffer = source.buffer();
                Charset UTF8 = Charset.forName("UTF-8");
                Log.i("REQUEST_JSON", buffer.clone().readString(UTF8));
                Log.i("REQUEST_URL", request.toString());
                return response;
            }
        };
        okHttpClient.addInterceptor(loggingIntercept);
        okHttpClient.addInterceptor(headerInterceptor);
        okHttpClient.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);//5,单位为秒
        return okHttpClient.build();
    }
}
